package com.suncnpap.library.controller;

import com.suncnpap.library.model.CollectionChange;
import com.suncnpap.library.model.Library;
import com.suncnpap.library.service.CollectionChangeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.Constants;
import com.app.util.common.EasyuiGrid;
import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.ExportExcelUtils;

/** 
 * 说明：书屋变更Controller
 */
@Controller
@RequestMapping(value="/library/collectionChange")
public class CollectionChangeController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(CollectionChangeController.class);
	
	
	@Autowired
	CollectionChangeService collectionChangeService;
	
	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			page = collectionChangeService.list(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<CollectionChange> list = page.getDataList();
				String header = "图书条码,正题名,变更前书屋,变更后书屋,操作日期,操作员";
				
				String tableStr = "strBookBarcode,strPositiveTitle,strSourceCollection,strNewCollection,dtCreatDate,strCreatName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("书屋变更", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody CollectionChange obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				collectionChangeService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody CollectionChange obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				collectionChangeService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					collectionChangeService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				CollectionChange obj = collectionChangeService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
}
